/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view.components;

import com.javaforge.tapestry.spring.annotations.InjectSpring;
import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Asset;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.services.DataSqueezer;
import org.braintrain.domain.Navigation;
import org.braintrain.services.NavigationService;
import org.braintrain.tapestry.encoders.ContentServiceEncoder;

import javax.servlet.ServletContext;
import java.util.List;


public abstract class NavigationChain extends BaseComponent {
    
    @Asset("/images/content.gif")
    public abstract IAsset getContentImage();
    @Asset("/images/test.gif")
    public abstract IAsset getTestImage();
    @Asset("/images/deletedcontent.gif")
    public abstract IAsset getDeletedContentImage();
    @Asset("/images/deletedtest.gif")
    public abstract IAsset getDeletedTestImage();
    @Asset("/images/editedcontent.gif")
    public abstract IAsset getEditedContentImage();
    @Asset("/images/editedtest.gif")
    public abstract IAsset getEditedTestImage();
    
    
    @Asset("/images/downgradient.gif")
    public abstract IAsset getDownImage();
    @Asset("/images/pageLink.gif")
    public abstract IAsset getPageLinkImage();
    @Asset("/images/popup.gif")
    public abstract IAsset getPopupImage();
    @Asset("/images/moveUp.gif")
    public abstract IAsset getMoveUpImage();
    @Asset("/images/moveDown.gif")
    public abstract IAsset getMoveDownImage();
    @Asset("/images/moveUpGrey.gif")
    public abstract IAsset getMoveUpGreyImage();
    @Asset("/images/moveDownGrey.gif")
    public abstract IAsset getMoveDownGreyImage();
    @Asset("/images/magnifier.gif")
    public abstract IAsset getPreviewImage();
    
    @InjectObject("service:tapestry.data.DataSqueezer")
    public abstract DataSqueezer getDataSqueezer();

    @InjectObject("service:tapestry.globals.ServletContext")
    public abstract ServletContext getServletContext();

    @Parameter(required = false, defaultValue = "null")
    public abstract String getChainCallback();
    public abstract void setChainCallback(String callback);

    @Parameter(required = false, defaultValue = "null")
    public abstract String getPopupCallback();
    public abstract void setPopupCallback(String callback);
    
    @Parameter(required=false)
    public abstract Long getCurrentNavigationId();
    public abstract void setCurrentNavigationId(Long id);
    
    @Parameter(required=true)
    public abstract List<Navigation> getSource();
    public abstract void setSource(List<Navigation> c);
    
    @Parameter(required=false)
    public abstract void setModuleId(Long moduleId);
    public abstract Long getModuleId();
    
    @Parameter(required=true)
    public abstract boolean isReorderable();
    public abstract void setReorderable(boolean v);
    
    public abstract void setNavigation(Navigation page);
    public abstract Navigation getNavigation();
    
    @InjectSpring("navigationService")
    public abstract NavigationService getNavigationService();
    
    public abstract int getChainIndex();
    public abstract void setChainIndex(int i);
    
    public IAsset getNavigationImage()
    {
        if(getNavigation().isTest())
        {
            if(getNavigation().isDeleted())
            {
                return getDeletedTestImage();
            }
            else if(getNavigation().isMoved() || 
                    getNavigation().isPending() || 
                    getNavigation().getNullSafeContent().getDeltaValue() != null)
            {
                return getEditedTestImage();
            }
            return getTestImage();
        }
        else
        {
            if(getNavigation().isDeleted())
            {
                return getDeletedContentImage();
            }
            else if(getNavigation().isMoved() || 
                    getNavigation().isPending() || 
                    getNavigation().getNullSafeContent().getDeltaValue() != null)
            {
                return getEditedContentImage();
            }
            return getContentImage();
        }
    }
    
    //reorder navigation elements
    public void moveNavBack(IRequestCycle cycle)
    {
        Object[] params = cycle.getListenerParameters();
        
        if(params.length > 1 && params[0] instanceof Long && params[1] instanceof Long)
        {
            setModuleId((Long)params[0]);
            
            getNavigationService().saveMoveNavigationBack((Long)params[1]);
        }
    }
    
    public boolean activateMoveNavBack()
    {
        return !(getNavigation().isDeleted() ||
               getNavigation().getDeltaPrev() == null);
    }
    
    public boolean activateMoveNavForward()
    {
        return !(getNavigation().isDeleted() ||
               getNavigation().getDeltaNext() == null);
    }
    
    public void moveNavForward(IRequestCycle cycle)
    {
        Object[] params = cycle.getListenerParameters();
        
        if(params.length > 1 && params[0] instanceof Long && params[1] instanceof Long)
        {
            setModuleId((Long)params[0]);
            
            getNavigationService().saveMoveNavigationForward((Long)params[1]);
        }
    }
    
    public String dotdot(String in, int end)
    {
        if(in.length() > end)
        {
            return in.substring(0, end) + "..";
        }
        return in;
    }
    
    public String generatePagePath(Navigation nav){
        return ContentServiceEncoder.buildPagePath(nav.getType(),getDataSqueezer().squeeze(new Long[]{nav.getId(), getCurrentNavigationId()}));
    }
    
    public String generateDirectPagePath(Navigation nav){
        return ContentServiceEncoder.buildDirectPagePath(getServletContext(), getDataSqueezer().squeeze(new Long[]{nav.getId()}));
    }
    
    public boolean isNavPreviewable()
    {
        return !getNavigation().isTest();
    }

    public boolean isChainDisabled()
    {
        return getSource().get(0).isDeleted();
    }

    public boolean isPopupDisabled()
    {
        return getNavigation().isTest() || getNavigation().isDeleted();
    }

    public boolean isLinkDisabled(){
        return getChainCallback() != null || getPopupCallback() != null;
    }


}